package com.iotstudio.embeddedcloudplatform.service.serviceImp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iotstudio.embeddedcloudplatform.POJO.DO.Sensor;
import com.iotstudio.embeddedcloudplatform.POJO.DO.SensorData;
import com.iotstudio.embeddedcloudplatform.util.page.DateUtil;
import com.iotstudio.embeddedcloudplatform.POJO.DO.UserSensor;
import com.iotstudio.embeddedcloudplatform.POJO.VO.ResultVO;
import com.iotstudio.embeddedcloudplatform.dao.SensorMapper;
import com.iotstudio.embeddedcloudplatform.service.SensorService;
import com.iotstudio.embeddedcloudplatform.util.KeyUtil;
import com.iotstudio.embeddedcloudplatform.util.page.ListUtil;
import com.iotstudio.embeddedcloudplatform.util.web.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SensorServiceImp implements SensorService {

    @Autowired
    private SensorMapper sensorMapper;

    @Override
    public ModelAndView add(UserSensor userSensor) {
        sensorMapper.addUserSensor(userSensor);
        String resourceName=sensorMapper.selectById(userSensor.getSensorId());
        List<ListUtil> Data=new ListUtil().tempList();
        int parentId=Data.get(0).getResourceID();
        int resourceId=Data.get(Data.size()-1).getResourceID()+1;
        ListUtil list=new ListUtil("http://localhost:8080/v1/pub/showData?sensorId=userSensor.getSensorId()",false,0,
                parentId,"","",
                2,resourceId,resourceName,
                0,"");
        Data.add(list);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/static/index.html");
        return modelAndView;
    }

    @Override
    public ResultVO deleteSensor(String id, String name) {
        sensorMapper.deleteByName(name);
        sensorMapper.deleteBySensorId(id);
        sensorMapper.deleteById(id);
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO dataTransmission(SensorData sensorData, String name) {
        String id = KeyUtil.genUniqueKey();
        sensorData.setId(id);
        String dataTypeId=sensorMapper.selectByName(name);
        sensorData.setDataTypeId(dataTypeId);
        sensorMapper.addData(sensorData);
        return ResultVOUtil.success();
    }

    @Override
    public Object showSensorData(String sensorId){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //当前时间精确到小时
        String CURRENT_TIME=format.format(new Date());
        //当天时间前一个星期
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -6);
        String START_TIME = format.format(c.getTime())+" 00:00:00";
        List<DateUtil> list=sensorMapper.selectDataUtil(sensorId,START_TIME,CURRENT_TIME);
        //list转json并返回
        return JSON.toJSON(list);
    }
    @Override
    public JSONArray getSensorData(String sensorId){
        int y,m,d;
        Calendar cal=Calendar.getInstance();
        y=cal.get(Calendar.YEAR);
        m=cal.get(Calendar.MONTH)+1;
        d=cal.get(Calendar.DATE);
        String time = String.format(y+"-"+m+"-"+d);
        System.out.println(time);
        JSONArray jsonArray = new JSONArray();
        List<SensorData> list = sensorMapper.selectAll(sensorId,time);
        for(int i = 0;i<list.size();i++){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("value",list.get(i).getValue());
            jsonObject.put("time",list.get(i).getTime());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }
    @Override
    public Sensor getSensor(String sensorId) {
        return sensorMapper.selectOne(sensorId);
    }
}
